import React, { PureComponent } from 'react';
import { Form, Input } from 'antd';
import { connect } from 'dva';

class PierForm extends PureComponent {
    constructor(props) {
        super(props);
        this.state = {  }
        this.onConfirm = this.onConfirm.bind(this);
    }

    onConfirm(params={}){
        const {form,addPier}=this.props;
        form.validateFields((error, values) => {
            if(!error){
                form.resetFields()
                addPier(values, (Pier) => {
                    if(params.onSuccess){
                        params.onSuccess(Pier);
                    }
                }, (error) => {
                    if(params.onError){
                        params.onError(error);
                    }
                });
            }else{
                if(params.onError){
                    params.onError(error);
                }
            }
        });
    }

    componentDidMount() {
        if(this.props.onLoaded){
            this.props.onLoaded(this);
        }
    }

    render() { 
        const {
            form={},
            mode='add'
        } = this.props;
        const { getFieldDecorator } = form;

        return (
            <Form 
                labelCol={{ span: 6 }}
                wrapperCol={{ span: 18 }}
            >
                <Form.Item label='ID' style={{ display: 'none' }}>
                    {getFieldDecorator('id', {
                        rules: [
                            { required: mode != 'add', message: '必填' }
                        ]
                    })(
                        <Input  />
                    )}
                </Form.Item>
                <Form.Item label='桥墩名称'>
                    {getFieldDecorator('cnName', {
                        rules: [
                            { required: true, message: '必填' }
                        ]
                    })(
                        <Input placeholder='桥墩名称'/>
                    )}
                </Form.Item>
                <Form.Item label='英文名称'>
                    {getFieldDecorator('enName', {
                        rules: [
                            { required: true, message: '必填' }
                        ]
                    })(
                        <Input placeholder='桥墩英文名称'/>
                    )}
                </Form.Item>
                <Form.Item label='编码'>
                    {getFieldDecorator('code', {
                        rules: [
                            { required: true, message: '必填' }
                        ]
                    })(
                        <Input placeholder='编码'/>
                    )}
                </Form.Item>
                <Form.Item label='墩号'>
                    {getFieldDecorator('pierNumber', {
                        rules: [
                            { required: true, message: '必填' }
                        ]
                    })(
                        <Input  placeholder='墩号'/>
                    )}
                </Form.Item>
                <Form.Item label='所属工程ID' style={{ display: 'none' }}>
                    {getFieldDecorator('projectID', {
                        rules: [
                            { required: true, message: '必填' }
                        ]
                    })(
                        <Input />
                    )}
                </Form.Item>
                <Form.Item label='里程桩长'>
                    {getFieldDecorator('mileagePileLength', {
                        rules: [
                            { required: true, message: '必填' }
                        ]
                    })(
                        <Input placeholder='里程桩长'/>
                    )}
                </Form.Item>
                <Form.Item label='斜交角'>
                    {getFieldDecorator('obliqueAngle', {
                        rules: [
                            { required: true, message: '必填' }
                        ]
                    })(
                        <Input placeholder='斜交角'/>
                    )}
                </Form.Item>
                <Form.Item label='桥面标高'>
                    {getFieldDecorator('bridgeDeckElevation', {
                        rules: [
                            { required: true, message: '必填' }
                        ]
                    })(
                        <Input placeholder='桥面标高'/>
                    )}
                </Form.Item>
                <Form.Item label='设计地面'>
                    {getFieldDecorator('bridgeDeckElevation', {
                        rules: [
                            { required: true, message: '必填' }
                        ]
                    })(
                        <Input placeholder='设计地面'/>
                    )}
                </Form.Item>
            </Form>
        );
    }
}
 
export default connect(
    ({ Pier: current }) => ({ current }),
    dispatch => ({
        addPier: (values, onSuccess, onError) => dispatch({
            type: 'Pier/add',
            payload: {
                onSuccess,
                onError,
                values
            }
        })
    })
)(Form.create('Pier', {

})(PierForm));